package com.kiki.stater.callbackdemo;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

/**
 * Created by mazeyi on 2020/12/10.
 */
public class CompleteFutureDemo {

    public static void ansyn() throws ExecutionException, InterruptedException {
        CompletableFuture<String> future = CompletableFuture.supplyAsync(new Supplier<String>(){
            @Override
            public String get() {
                try {
                    System.out.println(Thread.currentThread().getName()+"正在执行一个有返回值的异步任务。");
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "OK";
            }
        });
        String result=future.get();
        System.out.println(Thread.currentThread().getName()+"  结果："+result);
    }

    public static void main(String[] args) {
        try {
            ansyn();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}